package com.demo.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * HBase操作类
 *
 * @author lee9213@163.com
 * @version 1.0
 * @date 2017/3/14 16:01
 */
public class Client {

    private static final String tableName = "user";
    private static Connection conn = null;
    private static Configuration conf = null;

    static {
        conf = HBaseConfiguration.create();
        conf.set("hbase.Zookeeper.property.clientPort", "2181");
        conf.set("hbase.Zookerper.quorum", "192.168.80.130");
        conf.set("hbase.master", "192.168.80.130:60000");
//        try {
//            conn = ConnectionFactory.createConnection(conf);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    public static void createTable() throws IOException {
        HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
        if (hBaseAdmin.tableExists(tableName)) {
            hBaseAdmin.disableTable(tableName);
            hBaseAdmin.deleteTable(tableName);
        }
        HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
        tableDescriptor.addFamily(new HColumnDescriptor("info"));
        tableDescriptor.addFamily(new HColumnDescriptor("class"));
        tableDescriptor.addFamily(new HColumnDescriptor("parent"));
        hBaseAdmin.createTable(tableDescriptor);
        hBaseAdmin.close();
    }

    public static void putRow() throws IOException {
        String[] familyNames = {"info", "class", "parent"};
        HTable hTable = new HTable(conf, tableName);
        for (int i = 0; i < 20; i++) {
            for (int j = 0; j < familyNames.length; j++) {
                Put put = new Put(Bytes.toBytes(i + ""));
                put.add(Bytes.toBytes(familyNames[i]), Bytes.toBytes("col"), Bytes.toBytes("value_" + i + "_" + j));
                hTable.put(put);
            }
        }
        hTable.close();
    }

    public static void getRow() throws IOException {
        String rowKey = "1";
        HTable hTable = new HTable(conf, tableName);
        Get get = new Get(Bytes.toBytes(rowKey));
        Result result = hTable.get(get);
        outputResult(result);
        hTable.close();
    }

    public static void outputResult(Result result) {
        List<Cell> list = result.listCells();
        System.out.println("row key:" + new String(result.getRow()));
        for (Cell cell : list) {
            System.out.println("family:" + new String(cell.getFamilyArray()) + ", col:" + new String(cell.getQualifierArray())
                    + ", value:" + new String(cell.getValueArray()));
        }
    }

    public static void scanTable() throws IOException {
        HTable hTable = new HTable(conf, tableName);
        Scan scan = new Scan();
        ResultScanner resultScanner = hTable.getScanner(scan);
        for (Result result : resultScanner) {
            outputResult(result);
        }
        scan = new Scan("2".getBytes(), "6".getBytes());
        resultScanner = hTable.getScanner(scan);
        for (Result result : resultScanner) {
            outputResult(result);
        }
        hTable.close();
    }

    public static void deleteRow() throws IOException {
        String rowKey = "1";
        HTable hTable = new HTable(conf, tableName);
        List<Delete> list = new ArrayList<>();
        Delete delete = new Delete(rowKey.getBytes());
        list.add(delete);
        hTable.delete(list);
        hTable.close();
    }
}
